package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.*;
import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Category;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper mapper;
    @Autowired
    private BrandCategoryMapper brandCategoryMapper;
    @Autowired
    private CategoryAttributeTemplateMapper categoryAttributeTemplateMapper;
    @Autowired
    private SpuMapper spuMapper;

    public void addNew(CategoryAddNewDTO addNewDTO) {
        if (mapper.countByName(addNewDTO.getName())>0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"添加失败，类别名称已被占用！");
        }

        CategoryStandardVO categoryStandardVO = null;
        Integer depth = 1;
        Long parentId = addNewDTO.getParentId();
        if (parentId != 0){
            categoryStandardVO = mapper.getStandardById(parentId);
            if (categoryStandardVO==null){
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"添加失败，类别父级不存在！");
            }else {
                depth = categoryStandardVO.getDepth()+1;
            }
        }

        Category category = new Category();
        category.setDepth(depth);
        category.setIsParent(0);
        BeanUtils.copyProperties(addNewDTO,category);
        int insertRows = mapper.insert(category);
        if (insertRows != 1){
            String message = "添加类别失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        if (parentId !=0){
            if (categoryStandardVO.getIsParent()==0){
                Category updateCategory = new Category();
                updateCategory.setId(parentId);
                updateCategory.setIsParent(1);
                int updateRows = mapper.update(updateCategory);
                if (updateRows != 1){
                    String message = "更新类别失败，服务器忙，请稍后再次尝试！";
                    throw new ServiceException(ServiceCode.ERR_UPDATE,message);
                }
            }
        }
    }

    public void delete(Long id) {
        CategoryStandardVO currentCategory = mapper.getStandardById(id);
        if (currentCategory==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"删除失败，类别已不存在！");
        } else if (currentCategory.getIsParent()==1){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败，类别是父级类别");
        }else if (brandCategoryMapper.countByCategory(id)>0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败，当前类别关联到了某些品牌！");
        }else if (categoryAttributeTemplateMapper.countByCategory(id)>0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败，当前类别关联到了属性模板！");
        }else if (spuMapper.countByCategory(id)>0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败，当前类别关联到了spu！");
        }

        int deleteRows = mapper.deleteById(id);
        if (deleteRows != 1){
            String message = "删除类别失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        Long parentId = currentCategory.getParentId();
        if (parentId != 0){
            if (mapper.countByParentId(parentId) ==0 ){
                Category updateCategory = new Category();
                updateCategory.setId(parentId);
                updateCategory.setIsParent(0);
            }
        }
    }

    @Override
    public void setEnable(Long id) {
        updateEnableById(id,1);
    }

    @Override
    public void setDisable(Long id) {
        updateEnableById(id,0);
    }

    private void updateEnableById(Long id,Integer enable){
        String enableText[] = {"禁用","启用"};

        CategoryStandardVO queryResult =  mapper.getStandardById(id);
        if (queryResult == null){
            String message = enableText[enable] + "类别失败，尝试访问的类别数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        if (queryResult.getEnable() == enable){
            String message = enableText[enable] + "类别失败，当前类别已经处理" + enableText[enable] + "状态";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Category category = new Category();
        category.setId(id);
        category.setEnable(enable);
        int updateRows = mapper.update(category);
        if (updateRows != 1){
            String message = enableText[enable] + "类别失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public CategoryStandardVO getStandard(Long id) {
        CategoryStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO == null) {
            String message = "查询类别失败，该类别不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return standardVO;
    }

    @Override
    public List<CategoryListItemVO> listByParentId(Long parentId) {
        List<CategoryListItemVO> list = mapper.listByParentId(parentId);
        return list;
    }
}
